<?php
class system_file_image {
	private $img = null;
	private $path = null;
	private $error = false;
	private $types = array(1 => 'gif', 2 => 'jpg', 3 => 'png', 6 => 'bmp');
	
	public function __construct($img) {
		if(!file_exists($img)) {
			$this->error = true;
		} else {
			$path = pathinfo($img);
			$this->path = $path['dirname'];
			$this->img = $img;
		}
	}
	
	public function getImageInfo() {
		$info = @getimagesize($this->img);
		if(isset($this->types[$info[2]])) {
			$info['ext'] = $info['type'] = $this->types[$info[2]];
		} else {
			$info['ext'] = $info['type'] = 'jpg';
		}
		$info['type'] == 'jpg' && $info['type'] = 'jpeg';
		$info['size'] = @filesize($this->img);
		return $info;
	}
	
	/**
	 * 新图地址, 宽, 高, 裁剪, 允许放大
	 * @return system_file_image
	 */
	public function thumb($name, $new_w = 160, $new_h = 120, $cut = 0, $big = 0) {
		if($this->error) {
			throw new system_exception("Image file is not existing.");
		}
		// 获取原图信息
		$info = $this->getImageInfo();
		if(!empty($info[0])) {
			$old_w = $info[0];
			$old_h = $info[1];
			$type = $info['type'];
			$ext = $info['ext'];
			unset($info);
			$filename = $this->path . '/' . $name . '.' . $ext;
			// 如果原图比缩略图小 并且不允许放大
			if($old_w < $new_h && $old_h < $new_w && !$big) {
				copy($this->img, $filename);
				return $this;
			}
			// 裁剪图片
			if($cut == 0) { // 等比列
				$scale = min($new_w / $old_w, $new_h / $old_h); // 计算缩放比例
				$width = (int) ($old_w * $scale); // 缩略图尺寸
				$height = (int) ($old_h * $scale);
				$start_w = $start_h = 0;
				$end_w = $old_w;
				$end_h = $old_h;
			} elseif($cut == 1) { // center center 裁剪
				$scale1 = round($new_w / $new_h, 2);
				$scale2 = round($old_w / $old_h, 2);
				if($scale1 > $scale2) {
					$end_h = round($old_w / $scale1, 2);
					$start_h = ($old_h - $end_h) / 2;
					$start_w = 0;
					$end_w = $old_w;
				} else {
					$end_w = round($old_h * $scale1, 2);
					$start_w = ($old_w - $end_w) / 2;
					$start_h = 0;
					$end_h = $old_h;
				}
				$width = $new_w;
				$height = $new_h;
			} elseif($cut == 2) { // left top 裁剪
				$scale1 = round($new_w / $new_h, 2);
				$scale2 = round($old_w / $old_h, 2);
				if($scale1 > $scale2) {
					$end_h = round($old_w / $scale1, 2);
					$end_w = $old_w;
				} else {
					$end_w = round($old_h * $scale1, 2);
					$end_h = $old_h;
				}
				$start_w = 0;
				$start_h = 0;
				$width = $new_w;
				$height = $new_h;
			}
			// 载入原图
			$createFun = 'ImageCreateFrom' . $type;
			$oldimg = $createFun($this->img);
			// 创建缩略图
			if($type != 'gif' && function_exists('imagecreatetruecolor')) {
				$newimg = imagecreatetruecolor($width, $height);
			} else {
				$newimg = imagecreate($width, $height);
			}
			// 复制图片
			if(function_exists("ImageCopyResampled")) {
				ImageCopyResampled($newimg, $oldimg, 0, 0, $start_w, $start_h, $width, $height, $end_w, $end_h);
			} else {
				ImageCopyResized($newimg, $oldimg, 0, 0, $start_w, $start_h, $width, $height, $end_w, $end_h);
			}
			
			// 对jpeg图形设置隔行扫描
			$type == 'jpeg' && imageinterlace($newimg, 1);
			// 生成图片
			$imageFun = 'image' . $type;
			if(!@$imageFun($newimg, $filename)) {
				throw new system_exception("Save file faild,Please check if the folder is writable.");
			}
			ImageDestroy($newimg);
			ImageDestroy($oldimg);
			return $this;
		}
		return $this;
	}
	
}
